package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.health_2.R;

// 书籍详情
public class BookDetailActivity extends BaseActivity {
    private WebView webview;// 用于显示商品详情

    private String name = "书籍详情";// 传过来的书籍名称
    private String content = "<h4>暂无数据，换一本书看吧~</h4>";// 传过来的html代码

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_book_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview = findViewById(R.id.webview);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            if (!TextUtils.isEmpty(bundle.getString("name"))) {
                name = bundle.getString("name");
            }
            if (!TextUtils.isEmpty(bundle.getString("content"))) {
                content = bundle.getString("content");
            }
        }

        setHeadTitle(name);

        initWebview(content);
    }

    private void initWebview(String url) {
        // 开启js支持
        webview.getSettings().setJavaScriptEnabled(true);
        //2、关闭自动适应
        webview.getSettings().setUseWideViewPort(false);
        webview.getSettings().setLoadWithOverviewMode(false);
        // 3、//重写WebViewClient中的onPageFinished方法
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //修改图片大小
                int screenWidth = ScreenUtils.getScreenWidth();
//                String width = String.valueOf(screenWidth - SizeUtils.dp2px(50));
//                String width2 = String.valueOf(screenWidth - SizeUtils.dp2px(10));
                String width2 = String.valueOf(SizeUtils.px2dp(screenWidth) - 15);

                String javascript = "javascript:function ResizeImages() {" +
                        "var myimg,oldwidth;" +
                        "var maxwidth = document.body.clientWidth;" +
                        "for(i=0;i <document.images.length;i++){" +
                        "myimg = document.images[i];" +
//                        "myimg.margin = 0;" +
                        "if(myimg.width > " + width2 + "){" +
                        "oldwidth = myimg.width;" +
                        "myimg.width =" + width2 + ";" +
                        "}" +
                        "}" +
                        "}";
                view.loadUrl(javascript);
                view.loadUrl("javascript:ResizeImages();");
            }
        });
        webview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
//        webview.loadUrl(url);
        webview.loadData(url, "text/html;charset=utf-8", null); // 加载定义的代码，并设定编码格式和字符集。
    }
}
